package com.pega.tulokhorecorder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}
		String[] values = new String[] { "Andro", "iPhone", "WindowsMobile",
		        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
		        "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
		        "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
		        "Android", "iPhone", "WindowsMobile" };
		String[] values2 = new String[] { "1", "2", "3",
		        "4", "5", "6", "7", "8",
		        "9", "10", "11", "12", "13", "14",
		        "15", "16", "17", "18", "19", "20",
		        "21", "22", "23" };

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			ListView lv = (ListView) rootView.findViewById(R.id.list_view_main);
			final ArrayList<RowList> list = new ArrayList<RowList>();
		    
		    for (int i = 0; i < values.length; ++i) {
		    	RowList rowList = new RowList();
		    	rowList.title_name = values[i];
		    	rowList.time_name = values2[i];
		      list.add(rowList);
		    }
		    
			final StableArrayAdapter adapter = new StableArrayAdapter(getActivity(),
			        android.R.layout.simple_list_item_1, list);
			lv.setAdapter(adapter);
			return rootView;
		}
		private class StableArrayAdapter extends ArrayAdapter<RowList> {

		    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
		    private Context context;
			private List<RowList> object;
		    public StableArrayAdapter(Context context, int textViewResourceId,
		        List<RowList> objects) {
		    	
		    	
		      super(context, textViewResourceId,objects);
		      this.context = context;
		      this.object = objects;
		    }
		    @Override
		    public View getView(int position, View convertView, ViewGroup parent) {
		    	// TODO Auto-generated method stub
		    	LayoutInflater inflater = (LayoutInflater) context
		    	        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		    	View rowView = inflater.inflate(R.layout.list_view_main, parent, false);
		    	TextView title_list = (TextView) rowView.findViewById(R.id.title_list_id);
		    	TextView time_list = (TextView) rowView.findViewById(R.id.time_list_id);
		    	title_list.setText(object.get(position).title_name);
		    	time_list.setText(object.get(position).time_name);
 		    	return rowView;
		    }
		    @Override
		    public long getItemId(int position) {
		      //String item = getItem(position);
		      return position;
		    }
		    @Override
		    public boolean hasStableIds() {
		      return true;
		    }

		}
	}

	

}
